<?php
// +----------------------------------------------------------------------
// | zhanshop-marketing / Ad.php    [ 2024/12/23 15:15 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\admin\v1\controller;

use app\api\admin\Controller;
use app\provider\AdListenProvider;
use zhanshop\App;
use zhanshop\Helper;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Ad extends Controller
{
    /**
     * 验证token值
     * @param string $token
     * @return void
     * @throws \Exception
     */
    protected function checkToken(string $token)
    {
        //if(Helper::symmetryDecode($token) == false) App::error()->setError("TOKEN无效", 407);
    }
    /**
     * @api GET listen/{app}/{platform}/{event}/{token} 广告监测
     * @apiGroup 广告数据
     */
    public function getListen(Request $request, Response $response)
    {
        $app = $request->getData('app');
        $platform = $request->getData('platform');
        $event = $request->getData('event');

        $token = $request->getData('token', "");
        $this->checkToken($token);

        $result = App::make(AdListenProvider::class)->listen($app, $platform, $event, $request->param());

        if(is_string($result)){
            return $result;
        }
        return $this->result($result);
    }
    /**
     * @api POST listen/{app}/{platform}/{event}/{token} 广告监测
     * @apiGroup 广告数据
     */
    public function postListen(Request $request, Response $response)
    {
        $app = $request->getData('app');
        $platform = $request->getData('platform');
        $event = $request->getData('event');
        $token = $request->getData('token', "");

        $this->checkToken($token);

        $result = App::make(AdListenProvider::class)->listen($app, $platform, $event, $request->param(), $request->get());

        if(is_string($result)){
            return $result;
        }
        return $this->result($result);
    }

    /**
     * @api GET ipv6 获取IPV6
     * @apiGroup 广告数据
     */
    public function getIpv6(Request $request, Response $response)
    {
        $ip = $request->realIp();
        $result = [
            'ip' => $ip,
        ];
        return $this->result($result);
    }
}